package com.atguigu.web;

import com.atguigu.pojo.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientBookServlet extends BaseServlet {
    BookService bookService= new BookServiceImpl();
    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNoString=request.getParameter("pageNo");//当前页码
        Integer pageNo= WebUtils.parseInt(pageNoString,1);
        System.out.println("BookServlet 68:pageNo is:"+pageNo);

        //String pageSizeString=request.getParameter("pageNo");//数据条数
        //Integer pageSize=WebUtils.parseInt(pageSizeString, Page.PAGE_SIZE);//默认为4
        //System.out.println("BookServlet 72:pageSize is:"+pageSize);
        Integer pageSize= Page.PAGE_SIZE;

        Page page=bookService.page(pageNo,pageSize);
        if(pageNo<1){//防止用户输入越界
            response.sendRedirect("bookServlet?action=page&pageNo=1");
        }
        if(pageNo>page.getPageNo()){
            response.sendRedirect("bookServlet?action=page&pageNo="+page.getPageNo());
        }
        page.setUrl("client/bookServlet?action=page");//这个参数,被用在"跳转页地址url生成"时,action是page时,采用得到;也就是有分页需求时才用得到
        request.setAttribute("page",page);
        //response.sendRedirect("bookServlet?action=list");
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);
    }

    public void pageByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNoString=request.getParameter("pageNo");//当前页码
        Integer pageNo= WebUtils.parseInt(pageNoString,1);
        System.out.println("BookServlet 68:pageNo is:"+pageNo);

        //String pageSizeString=request.getParameter("pageNo");//数据条数
        //Integer pageSize=WebUtils.parseInt(pageSizeString, Page.PAGE_SIZE);//默认为4
        //System.out.println("BookServlet 72:pageSize is:"+pageSize);
        Integer pageSize= Page.PAGE_SIZE;
        Integer min=WebUtils.parseInt(request.getParameter("min"),0);
        Integer max=WebUtils.parseInt(request.getParameter("max"),999999999);
        //System.out.println("min:"+min);
        //System.out.println("max:"+max);
        request.setAttribute("min",min);
        request.setAttribute("max",max);

        Page page=bookService.pageByPrice(pageNo,pageSize,min,max);
        if(pageNo<1){//防止用户输入越界
            response.sendRedirect("bookServlet?action=pageByPrice&pageNo=1");
        }
        if(pageNo>page.getPageNo()){
            response.sendRedirect("bookServlet?action=pageByPrice&pageNo="+page.getPageNo());
        }
        //看传参,自动发送分页url
        if(request.getParameter("min")!=null && request.getParameter("max")!=null){
            page.setUrl("client/bookServlet?action=pageByPrice&min="+min+"&max="+max);//这个参数,被用在"跳转页地址url生成"时,action是page时,采用得到;也就是有分页需求时才用得到
        }else{
            page.setUrl("client/bookServlet?action=pageByPrice");//这个参数,被用在"跳转页地址url生成"时,action是page时,采用得到;也就是有分页需求时才用得到
        }
        request.setAttribute("page",page);
        request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //System.out.println("action is:"+request.getParameter("action"));
        doPost(request,response);//这样能按照action的参数来执行同名的方法
    }
}
